/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.ToaTau_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class ToaTau_DAO {
    
    public static String Get_MaToa() throws SQLException{
        String MaToa = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "TO");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaToa = Rs.getString(1);
        }
        return MaToa;
    }
    
    public static ArrayList<ToaTau_DTO> GetALL()
    {
        ArrayList<ToaTau_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from ToaTau" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                ToaTau_DTO Toa = new ToaTau_DTO();
                Toa.setMaToa(rs.getString("MaToa"));
                Toa.setTenToa(rs.getString("TenToa"));
                Toa.setLoaiToa(rs.getInt("LoaiToa"));
                Toa.setSoLuongCho(rs.getInt("SoLuongCho"));
                Toa.setMaDT(rs.getString("MaDT"));
                ds.add(Toa);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(ToaTau_DTO Toa)
    {
        boolean flag = false;
        try {
            String sql = "Insert ToaTau Values('"+Toa.getMaToa()+"','"+Toa.getTenToa()+"','"+Toa.getLoaiToa()+"','"+ Toa.getSoLuongCho() +"','"+Toa.getMaDT()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(ToaTau_DTO Toa)
    {
        boolean flag = false;
        try {
            String sql = "Update ToaTau Set TenToa ='"+ Toa.getTenToa() +"', LoaiToa = "+ Toa.getLoaiToa() +", SoLuongCho = "+Toa.getSoLuongCho()+", MaDT = '"+Toa.getMaDT()+"' where MaToa = '"+ Toa.getMaToa()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maToa)
    {
        boolean flag = false;
        try {
            String sql = "Delete from ToaTau where MaToa = '"+maToa+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
